package com.ruoyix.novel.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyix.common.utils.StringUtils;
import com.ruoyix.common.core.page.TableDataInfo;
import com.ruoyix.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyix.novel.domain.bo.NovelWorldSettingBo;
import com.ruoyix.novel.domain.vo.NovelWorldSettingVo;
import com.ruoyix.novel.domain.NovelWorldSetting;
import com.ruoyix.novel.mapper.NovelWorldSettingMapper;
import com.ruoyix.novel.service.INovelWorldSettingService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 世界观设定Service业务层处理
 *
 * @author ruoyi
 * @date 2025-06-05
 */
@RequiredArgsConstructor
@Service
public class NovelWorldSettingServiceImpl implements INovelWorldSettingService {

    private final NovelWorldSettingMapper baseMapper;

    /**
     * 查询世界观设定
     */
    @Override
    public NovelWorldSettingVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询世界观设定列表
     */
    @Override
    public TableDataInfo<NovelWorldSettingVo> queryPageList(NovelWorldSettingBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<NovelWorldSetting> lqw = buildQueryWrapper(bo);
        Page<NovelWorldSettingVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询世界观设定列表
     */
    @Override
    public List<NovelWorldSettingVo> queryList(NovelWorldSettingBo bo) {
        LambdaQueryWrapper<NovelWorldSetting> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<NovelWorldSetting> buildQueryWrapper(NovelWorldSettingBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<NovelWorldSetting> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getProjectId() != null, NovelWorldSetting::getProjectId, bo.getProjectId());
        lqw.like(StringUtils.isNotBlank(bo.getName()), NovelWorldSetting::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getContent()), NovelWorldSetting::getContent, bo.getContent());
        return lqw;
    }

    /**
     * 新增世界观设定
     */
    @Override
    public Boolean insertByBo(NovelWorldSettingBo bo) {
        NovelWorldSetting add = BeanUtil.toBean(bo, NovelWorldSetting.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改世界观设定
     */
    @Override
    public Boolean updateByBo(NovelWorldSettingBo bo) {
        NovelWorldSetting update = BeanUtil.toBean(bo, NovelWorldSetting.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(NovelWorldSetting entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除世界观设定
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 根据项目ID查询世界观设定
     */
    @Override
    public NovelWorldSettingVo queryByProjectId(Long projectId) {
        LambdaQueryWrapper<NovelWorldSetting> lqw = Wrappers.lambdaQuery();
        lqw.eq(NovelWorldSetting::getProjectId, projectId);
        return baseMapper.selectVoOne(lqw);
    }
}
